xlv.imp.xml={
	implement:function(){
		this.implement('xlv.imp.ajax');
	},
	method:{
		xml_properties:{
			currentXML:null,
			responseXML:null
		},
		xml_request:function(url,options){
			this.ajax_request(url,options||{});
		},
		xml_loadFromString:function(str){
			var p=this.xml_properties;
			p.currentXML=this.xml_parseFromString(str);
			if(Prototype.Browser.IE)p.currentXML.setProperty('SelectionLanguage','XPath');
		},
		xml_parseFromString:function(str){
			if(Prototype.Browser.IE){
				var dom=new ActiveXObject('Msxml2.DOMDocument');
				dom.async=false;
				dom.loadXML(str);
				return dom;
			}
			return (new DOMParser()).parseFromString(str, "text/xml");
		},
		xml_readyState:function(){
			this.ajax_readyState();
		},
		xml_XMLReady:function(){
			return this.xml_properties.currentXML!=null;
		},
		/**common service IE and firefox**/
		xml_selectNodes:function(xpath,node){
			node=(node||this.xml_properties.currentXML.documentElement);
			//firefox
			if(node.ownerDocument.evaluate){
    			var q = node.ownerDocument.evaluate(xpath,node,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    			var r = [];
			    for(var i=0,n = q.snapshotLength; i < n; i++)r.push(q.snapshotItem(i));
    			return r;
			}
			return node.selectNodes(xpath);
		},
		xml_selectSingleNode:function(xpath,node){
			//if(!node && !this.xml_properties.currentXML) alert('xpath :'+xpath);
			node=(node||this.xml_properties.currentXML.documentElement);
			if(node.ownerDocument.evaluate){
    			var q=node.ownerDocument.evaluate(xpath,node,null,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
				return (q)? q.singleNodeValue : null;
			}
			return node.selectSingleNode(xpath);
		},
		xml_adoptNode:function(node){
			var doc=this.xml_getDocument();
			return (doc.adoptNode&&node.ownerDocument!=doc)?doc.adoptNode(node):node;
		},
		xml_getNodeValue:function(node){
			if(node.nodeType==2||node.nodeType==3) return node.nodeValue;
			var value='';
			var nodes=node.childNodes;
			for(var i=0,n=nodes.length;i<n;++i)	value += (nodes[i].nodeType==3)?nodes[i].nodeValue :'';
			return value;
		},
		xml_setNodeValue:function(node,value){
			try{
				if(!node||value==null) return;
				if(node.nodeType==2||node.nodeType==3) node.nodeValue=value;
				else if(node.nodeType==1){
					var nodes=node.childNodes;
					var text_node=null;
					for(var i=nodes.length-1;i>=0;--i){
						if(nodes[i].nodeType==3) {
							if(text_node)node.removeChild(text_node);
							text_node=nodes[i];
						}
					}
					if(text_node)text_node.nodeValue=value;
					else node.insertBefore(node.ownerDocument.createTextNode(value),node.firstChild);
				}
			}catch(e){alert('xml_setNodeValue :'+e);}
		},
		xml_getNodeText:function(node){
			return node?(Prototype.Browser.IE)?node.text:node.textContent : '';
		},
		xml_setNodeText:function(node,text){
			if(Prototype.Browser.IE)node.text=text;
			else node.textContent=text;
		},
		xml_transformNode:function(node,XSL){
			//IE
			if(Prototype.Browser.IE){
				var dom=new ActiveXObject('Msxml2.DOMDocument');
				dom.async=false;
				dom.loadXML(XSL);
				return node.transformNode(dom);
			}
			//firefox
			var parser = new DOMParser();
			var doc = parser.parseFromString(XSL, "text/xml");
			var xp=new XSLTProcessor();
			xp.importStylesheet(doc);
			var f=xp.transformToFragment(node,document);
			return f;
		},
		xml_documentElement:function(){
			return this.xml_properties.currentXML.documentElement;
		},
		xml_getDocument:function(){
			return this.xml_properties.currentXML != null ?  this.xml_properties.currentXML :
				   Prototype.Browser.IE ? new ActiveXObject('Msxml2.DOMDocument') : 
				   document.implementation.createDocument("", "", null);
		},
		xml_nodeToString:function(node){
			return (Prototype.Browser.IE)?node.xml : new XMLSerializer().serializeToString(node);
		},
		xml_stringToNode:function(str){
			return this.xml_parseFromString(str).documentElement;
		},
		xml_nodeIndexes:function(node,root_node){
			root_node=(root_node||this.xml_properties.currentXML.documentElement);
			var indexes='';
			while(node && node != root_node){
				var ns=this.xml_selectNodes('preceding-sibling::*',node);
				var c=(ns==null)? 1 : ns.length+1;
				if(c>0)indexes=c+((indexes=='')?'':'.')+indexes;
				node=this.xml_selectSingleNode('parent::*', node);
			}
			return indexes;
		},
		xml_createElement:function(tag_name,value){
			var element=this.xml_getDocument().createElement(tag_name);
			if(value)this.xml_setNodeValue(element,value);
			return element;
		},
		xml_createElements:function(tag_names, value){
			var names = tag_names.split(/\./);
			var current,first;
			var elements=[];
			for(var i = 0; i < names.length; ++i){
				if(names[i].strip().length==0)continue;
				var n = this.xml_createElement(names[i]);
				if(!first) first=n;
				if(current) current.appendChild(n);
				current=n;
				elements.push(n);
			}
			if(current && (value && value.strip().length > 0))this.xml_setNodeValue(current,value);
			return elements;
		},
		xml_responseMethodNames:function(){
			var p=this.xml_properties;
			if(p.responseXML) return p.responseXML.documentElement.getAttribute('methodNames');
		},
		/**be overrided **/
		xml_response:function(){
		},
		/**override ajax*/
		ajax_response:function(aT,aJ){
			var p=this.xml_properties;
			p.responseXML=aT.responseXML;
			if(Prototype.Browser.IE)p.responseXML.setProperty('SelectionLanguage','XPath');
			if(p.currentXML == null) p.currentXML=aT.responseXML;
			this.xml_response();
		}
	}
};

